From: Marius Hoch Date: Sun, 17 Jan 2016 17:03:25 +0000 (+0100) Subject: Add debug logging for the case that the API goes read only X-Git-Tag: 1.31.0-rc.0~8024^2 X-Git-Url: http://git.cyclocoop.org/%7D%7Cconcat%7B?a=commitdiff_plain;h=c582d477b3f655f2a2ded1e8d5fed25f837e3c21;p=lhc%2Fweb%2Fwiklou.git Add debug logging for the case that the API goes read only Useful for tasks like T123867. Change-Id: I39a65047eac05b9e0268a9184b9fae4c48e66ce9 --- diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 6ddc28af21..814d14e5de 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -1180,27 +1180,44 @@ class ApiMain extends ApiBase { && in_array( 'bot', $this->getUser()->getGroups() ) && wfGetLB()->getServerCount() > 1 ) { - // Figure out how many servers have passed the lag threshold - $numLagged = 0; - $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' ); - foreach ( wfGetLB()->getLagTimes() as $lag ) { - if ( $lag > $lagLimit ) { - ++$numLagged; - } - } - // If a majority of slaves are too lagged then disallow writes - $slaveCount = wfGetLB()->getServerCount() - 1; - if ( $numLagged >= ceil( $slaveCount / 2 ) ) { - $parsed = $this->parseMsg( array( 'readonlytext' ) ); - $this->dieUsage( - $parsed['info'], - $parsed['code'], - /* http error */ - 0, - array( 'readonlyreason' => "Waiting for $numLagged lagged database(s)" ) - ); + $this->checkBotReadOnly(); + } + } + + /** + * Check whether we are readonly for bots + */ + private function checkBotReadOnly() { + // Figure out how many servers have passed the lag threshold + $numLagged = 0; + $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' ); + $laggedServers = array(); + $loadBalancer = wfGetLB(); + foreach ( $loadBalancer->getLagTimes() as $serverIndex => $lag ) { + if ( $lag > $lagLimit ) { + ++$numLagged; + $laggedServers[] = $loadBalancer->getServerName( $serverIndex ) . " ({$lag}s)"; } } + + // If a majority of slaves are too lagged then disallow writes + $slaveCount = wfGetLB()->getServerCount() - 1; + if ( $numLagged >= ceil( $slaveCount / 2 ) ) { + $laggedServers = join( ', ', $laggedServers ); + wfDebugLog( + 'api-readonly', + "Api request failed as read only because the following DBs are lagged: $laggedServers" + ); + + $parsed = $this->parseMsg( array( 'readonlytext' ) ); + $this->dieUsage( + $parsed['info'], + $parsed['code'], + /* http error */ + 0, + array( 'readonlyreason' => "Waiting for $numLagged lagged database(s)" ) + ); + } } /**